<template>
  <div>
    <!-- {{schooluser}} -->

    <!-- {{schoolUser}} -->
    <!-- {{getUser}} -->
    <div class="toRight" style="float:right;margin-bottom:15px;">
      <el-button size="small" type="primary" :disabled="!disabled" @click="update(false)">添加负责人</el-button>
      <el-button size="small" type="primary" :disabled="disabled" @click="update(true)">修改负责人</el-button>
    </div>

    <el-dialog title="负责人" :visible.sync="dialogVisible" width="450px">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="管理员姓名" prop="name">
          <el-input v-model="ruleForm.name" placeholder="请输入管理员姓名，最长20个汉字"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phonenum">
          <el-input v-model="ruleForm.phonenum" placeholder="请输入手机号码"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="所属地区">
          <el-cascader
            :options="address()"
            :props="{value:'label'}"
            v-model="selectedOptions"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" :disabled="isup" @click="updataschool">确 定</el-button>
      </span>
    </el-dialog>

    <template>
      <el-table :data="schooluser" border style="width: 100%">
        <el-table-column prop="ID" label="序号" width="180"></el-table-column>
        <el-table-column prop="name" label="姓名" width="180"></el-table-column>
        <el-table-column prop="sex" label="性别"></el-table-column>
        <el-table-column prop="phonenum" label="手机号"></el-table-column>
        <el-table-column prop="type" label="角色"></el-table-column>
      </el-table>
    </template>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import {
  asyncgetschooluserbysid,
  asyncupdataschool
} from "../../../plugins/api.js";
export default {
  name: "school_teacher",
  props: ["ID"],
  data() {
    return {
      dialogVisible: false,
      ruleForm: {
        name: "",
        address: {},
        phonenum: "",
        schoolid: "",
        password: 123456
      },
      schooluser: [],
      getoption: [],
      selectedOptions: [],
      disabled: false,
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        phonenum: [
          { required: true, message: "请输入手机号", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      },
      isup: false
    };
  },
  computed: mapGetters(["getUser"]),
  mounted() {
    this.start();
  },
  watch: {
    $route() {
      this.start();
    }
  },
  methods: {
    async start() {
      this.schooluser = await asyncgetschooluserbysid({
        schoolid: this.ID
      });
      this.disabled = !this.schooluser.length;
    },
    async update(i) {
      if (!i) {
        this.ruleForm = {
          name: "",
          address: {},
          phonenum: "",
          schoolid: this.ID,
          password: 123456
        };
      } else {
        const {
          name,
          password,
          ID,
          schoolid,
          address,
          phonenum
        } = this.schooluser.find(v => v.type === "root");
        this.ruleForm = {
          ID,
          name,
          address,
          phonenum,
          schoolid,
          password
        };
        const addr = address ? address : {};
        this.selectedOptions[0] = addr.province;
        this.selectedOptions[1] = addr.city;
        this.selectedOptions[2] = addr.area;
      }

      this.dialogVisible = true;
    },
    async updataschool() {
      this.isup = true;
      const { ID } = this.ruleForm;
      let t = "添加";
      if (ID) {
        t = "修改";
      }
      try {
        await asyncupdataschool(this.ruleForm);
        await this.start();
        this.dialogVisible = false;
        this.$message.success(`${t}成功`);
      } catch (err) {
        this.$message.error(`${t}失败:${err}`);
      }
      this.isup = false;
    },
    address() {
      const address = window.address || [];
      return address;
    },
    handleChange() {
      const data = {
        province: this.selectedOptions[0],
        city: this.selectedOptions[1],
        area: this.selectedOptions[2]
      };
      this.ruleForm.address = data;
    },
    remoteMethod(query) {
      if (query !== "") {
        this.getoption = this.getUser.filter(item => {
          if (item.phonenum) {
            return item.phonenum.toString().indexOf(query) > -1;
          } else {
            return false;
          }
        });
      } else {
        this.options = [];
      }
    }
  }
};
</script>

<style scoped>
</style>